/**
 * 班级管理API
 * 处理班级CRUD、学生管理、班主任分配等相关接口
 */

import request from '@/utils/request'

// 数据类型定义
interface EduClass {
  id: string
  className: string
  grade: number
  description?: string
  status: 'ACTIVE' | 'INACTIVE'
  teacherId?: string
  teacherName?: string
  studentCount: number
  createdTime: string
  updatedTime?: string
}

interface Student {
  id: string
  studentId: string
  name: string
  gender: 'MALE' | 'FEMALE'
  classId: string
  className?: string
  parentIds: string[]
  status: 'ACTIVE' | 'INACTIVE'
  createdTime: string
}

interface ClassCreateRequest {
  className: string
  grade: number
  description?: string
  teacherId?: string
  status?: 'ACTIVE' | 'INACTIVE'
}

interface ClassUpdateRequest {
  className?: string
  grade?: number
  description?: string
  teacherId?: string
  status?: 'ACTIVE' | 'INACTIVE'
}

interface ClassListParams {
  status?: 'ACTIVE' | 'INACTIVE'
  grade?: number
  teacherId?: string
  page?: number
  pageSize?: number
  keyword?: string
}

interface ApiResponse<T> {
  code: number
  message: string
  data: T
  timestamp: string
}

interface PageResponse<T> {
  list: T[]
  total: number
  page: number
  pageSize: number
}

interface ClassStatistics {
  totalStudents: number
  activeStudents: number
  maleStudents: number
  femaleStudents: number
  averageAttendance: number
}

/**
 * 班级管理API类
 */
class ClassAPI {
  /**
   * 获取班级列表
   */
  async getClasses(params: ClassListParams = {}): Promise<ApiResponse<PageResponse<EduClass>>> {
    const queryParams: Record<string, any> = {
      page: params.page || 1,
      pageSize: params.pageSize || 20
    }

    if (params.status) queryParams.status = params.status
    if (params.grade) queryParams.grade = params.grade
    if (params.teacherId) queryParams.teacherId = params.teacherId
    if (params.keyword) queryParams.keyword = params.keyword

    return request.get<PageResponse<EduClass>>('/api/classes', queryParams, {
      showLoading: true,
      loadingText: '获取班级列表...',
      showError: true
    })
  }

  /**
   * 根据ID获取班级信息
   */
  async getClassById(classId: string): Promise<ApiResponse<EduClass>> {
    return request.get<EduClass>(`/api/classes/${classId}`, {}, {
      showLoading: true,
      loadingText: '获取班级信息...',
      showError: true
    })
  }

  /**
   * 创建班级
   */
  async createClass(classData: ClassCreateRequest): Promise<ApiResponse<EduClass>> {
    return request.post<EduClass>('/api/classes', classData, {
      showLoading: true,
      loadingText: '创建班级...',
      showError: true
    })
  }

  /**
   * 更新班级信息
   */
  async updateClass(classId: string, classData: ClassUpdateRequest): Promise<ApiResponse<EduClass>> {
    return request.put<EduClass>(`/api/classes/${classId}`, classData, {
      showLoading: true,
      loadingText: '更新班级信息...',
      showError: true
    })
  }

  /**
   * 删除班级
   */
  async deleteClass(classId: string): Promise<ApiResponse<void>> {
    return request.request<void>({
      url: `/api/classes/${classId}`,
      method: 'DELETE',
      showLoading: true,
      loadingText: '删除班级...',
      showError: true
    })
  }

  /**
   * 根据状态获取班级列表
   */
  async getClassesByStatus(status: string): Promise<ApiResponse<EduClass[]>> {
    return request.get<EduClass[]>(`/api/classes/status/${status}`, {}, {
      showLoading: true,
      loadingText: '获取班级列表...',
      showError: true
    })
  }

  /**
   * 根据年级获取班级列表
   */
  async getClassesByGrade(grade: number): Promise<ApiResponse<EduClass[]>> {
    return request.get<EduClass[]>(`/api/classes/grade/${grade}`, {}, {
      showLoading: true,
      loadingText: '获取班级列表...',
      showError: true
    })
  }

  /**
   * 根据班主任获取班级列表
   */
  async getClassesByTeacher(teacherId: string): Promise<ApiResponse<EduClass[]>> {
    return request.get<EduClass[]>(`/api/classes/teacher/${teacherId}`, {}, {
      showLoading: true,
      loadingText: '获取班级列表...',
      showError: true
    })
  }

  /**
   * 获取班级的所有学生
   */
  async getStudentsByClass(classId: string): Promise<ApiResponse<Student[]>> {
    return request.get<Student[]>(`/api/classes/${classId}/students`, {}, {
      showLoading: true,
      loadingText: '获取学生列表...',
      showError: true
    })
  }

  /**
   * 获取班级的班主任信息
   */
  async getClassTeacher(classId: string): Promise<ApiResponse<any>> {
    return request.get<any>(`/api/classes/${classId}/teacher`, {}, {
      showLoading: true,
      loadingText: '获取班主任信息...',
      showError: true
    })
  }

  /**
   * 添加单个学生到班级
   */
  async addStudentToClass(classId: string, studentId: string): Promise<ApiResponse<void>> {
    return request.post<void>(`/api/classes/${classId}/add-student`,
      { studentId },
      {
        showLoading: true,
        loadingText: '添加学生到班级...',
        showError: true
      }
    )
  }

  /**
   * 从班级移除单个学生
   */
  async removeStudentFromClass(classId: string, studentId: string): Promise<ApiResponse<void>> {
    return request.request<void>({
      url: `/api/classes/${classId}/remove-student/${studentId}`,
      method: 'DELETE',
      showLoading: true,
      loadingText: '从班级移除学生...',
      showError: true
    })
  }

  /**
   * 批量添加学生到班级
   */
  async addStudentsToClass(classId: string, studentIds: string[]): Promise<ApiResponse<void>> {
    return request.post<void>(`/api/classes/${classId}/add-students`,
      { studentIds },
      {
        showLoading: true,
        loadingText: '批量添加学生到班级...',
        showError: true
      }
    )
  }

  /**
   * 批量从班级移除学生
   */
  async removeStudentsFromClass(classId: string, studentIds: string[]): Promise<ApiResponse<void>> {
    return request.request<void>({
      url: `/api/classes/${classId}/remove-students`,
      method: 'DELETE',
      data: { studentIds },
      showLoading: true,
      loadingText: '批量移除学生...',
      showError: true
    })
  }

  /**
   * 分配班主任
   */
  async assignTeacher(classId: string, teacherId: string): Promise<ApiResponse<void>> {
    return request.put<void>(`/api/classes/${classId}/assign-teacher`,
      { teacherId },
      {
        showLoading: true,
        loadingText: '分配班主任...',
        showError: true
      }
    )
  }

  /**
   * 移除班主任
   */
  async removeTeacher(classId: string): Promise<ApiResponse<void>> {
    return request.put<void>(`/api/classes/${classId}/remove-teacher`, {}, {
      showLoading: true,
      loadingText: '移除班主任...',
      showError: true
    })
  }

  /**
   * 激活班级
   */
  async activateClass(classId: string): Promise<ApiResponse<void>> {
    return request.put<void>(`/api/classes/${classId}/activate`, {}, {
      showLoading: true,
      loadingText: '激活班级...',
      showError: true
    })
  }

  /**
   * 停用班级
   */
  async deactivateClass(classId: string): Promise<ApiResponse<void>> {
    return request.put<void>(`/api/classes/${classId}/deactivate`, {}, {
      showLoading: true,
      loadingText: '停用班级...',
      showError: true
    })
  }

  /**
   * 获取班级统计信息
   */
  async getClassStatistics(classId: string): Promise<ApiResponse<ClassStatistics>> {
    return request.get<ClassStatistics>(`/api/classes/${classId}/statistics`, {}, {
      showLoading: true,
      loadingText: '获取班级统计...',
      showError: true
    })
  }

  /**
   * 获取所有年级
   */
  async getGrades(): Promise<ApiResponse<number[]>> {
    return request.get<number[]>('/api/classes/grades', {}, {
      showLoading: false,
      showError: true
    })
  }

  /**
   * 批量操作班级状态
   */
  async batchUpdateClassStatus(classIds: string[], status: 'ACTIVE' | 'INACTIVE'): Promise<ApiResponse<void>> {
    return request.post<void>('/api/classes/batch-update-status', {
      classIds,
      status
    }, {
      showLoading: true,
      loadingText: '批量更新班级状态...',
      showError: true
    })
  }

  /**
   * 批量删除班级
   */
  async batchDeleteClasses(classIds: string[]): Promise<ApiResponse<void>> {
    return request.request<void>({
      url: '/api/classes/batch-delete',
      method: 'POST',
      data: { classIds },
      showLoading: true,
      loadingText: '批量删除班级...',
      showError: true
    })
  }
}

// 创建API实例
export const classAPI = new ClassAPI()

// 导出类型
export type {
  EduClass,
  Student,
  ClassCreateRequest,
  ClassUpdateRequest,
  ClassListParams,
  ClassStatistics,
  PageResponse
}